﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ICeFramework.Common.DataLayer;

namespace ICeFramework.Business.DataLayer
{
    public class EntityColumnCollection<TEntity, TEntityRow> : ICollection<EntityColumn>
        where TEntity : class
        where TEntityRow : class
    {
        private List<EntityColumn> _list = new List<EntityColumn>();
        private EntityBase<TEntity, TEntityRow> _entity;

        #region public EntityColumn this[int columnIndex]
        /// <summary>
        /// Gets the <see cref="EntityColumn"/> item identified by the given arguments of the EntityColumnCollection
        /// </summary>
        /// <value></value>
        public EntityColumn this[int columnIndex]
        {
            get { return _list[columnIndex]; }
        }
        #endregion

        #region public EntityColumnCollection(EntityBase<TEntity, TEntityRow> entity)
        /// <summary>
        /// Initializes a new instance of the <b>EntityColumnCollection&lt;TEntity, TEntityRow&gt;</b> class.
        /// </summary>
        /// <param name="entity"></param>
        public EntityColumnCollection(EntityBase<TEntity, TEntityRow> entity)
        {
            _entity = entity;
        }
        #endregion

        #region ICollection<BaseColumn> Members

        public void Add(EntityColumn item)
        {
            _list.Add(item);
            _entity.DataTable.Columns.Add(item.Name, item.ColumnType);
        }

        public void Clear()
        {
            _list.Clear();
        }

        public bool Contains(EntityColumn item)
        {
            return _list.Contains(item);
        }

        public void CopyTo(EntityColumn[] array, int arrayIndex)
        {
            _list.CopyTo(array, arrayIndex);
        }

        public int Count
        {
            get { return _list.Count; }
        }

        public bool IsReadOnly
        {
            get { return false; }
        }

        public bool Remove(EntityColumn item)
        {
            bool b = _list.Remove(item);
            if (b)
            {
                _entity.DataTable.Columns.Remove(item.Name);
            }
            return b;
        }

        #endregion

        #region IEnumerable<BaseColumn> Members

        public IEnumerator<EntityColumn> GetEnumerator()
        {
            return _list.GetEnumerator();
        }

        #endregion

        #region IEnumerable Members

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return _list.GetEnumerator();
        }

        #endregion
    }
}
